package com.wzx.data.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzx.basics.baseVo.PageVo;
import com.wzx.basics.baseVo.Result;
import com.wzx.basics.utils.PageInfo;
import com.wzx.basics.utils.PageUtil;
import com.wzx.basics.utils.ResultUtil;
import com.wzx.data.entity.CourseWare;
import com.wzx.data.entity.Teacher;
import com.wzx.data.utils.NullUtils;
import com.wzx.data.entity.Question;
import com.wzx.data.service.IQuestionService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wzx.data.vo.CourseWareVo;
import com.wzx.data.vo.QuestionVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 提问 前端控制器
 * @author wzx
 * @since 2023/03/20
 */
@Slf4j
@RestController
@Api(tags = "提问管理接口")
@RequestMapping("/wzx/question")
@Transactional
public class QuestionController {

    @Autowired
    private IQuestionService iQuestionService;

    @RequestMapping(value = "/getOne", method = RequestMethod.GET)
    @ApiOperation(value = "查询单条提问")
    public Result<Question> get(@RequestParam String id){
        return new ResultUtil<Question>().setData(iQuestionService.getById(id));
    }

    @RequestMapping(value = "/count", method = RequestMethod.GET)
    @ApiOperation(value = "查询全部提问个数")
    public Result<Long> getCount(){
        return new ResultUtil<Long>().setData(iQuestionService.count());
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "查询全部提问")
    public Result<List<Question>> getAll(){
        return new ResultUtil<List<Question>>().setData(iQuestionService.list());
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "查询提问")
    public Result getByPage(@ModelAttribute QuestionVo vo ,@ModelAttribute PageVo pageVo){
        System.out.println(vo);
        final LambdaQueryWrapper<Question> lambda = new QueryWrapper<Question>().lambda();
        this.buildCondition(lambda,vo);
        final IPage<Question> page = iQuestionService.page(new Page<>(pageVo.getPageNumber(), pageVo.getPageSize()), lambda);
        PageInfo<Question> pageInfo = new PageInfo<>(pageVo.getPageNumber(), pageVo.getPageSize());
        pageInfo.setTotal((int) page.getTotal());
        pageInfo.setTotalList(page.getRecords());
        return ResultUtil.data(pageInfo);
    }



    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ApiOperation(value = "新增提问")
    public Result<Question> insert(Question question){
        iQuestionService.saveOrUpdate(question);
        return new ResultUtil<Question>().setData(question);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "编辑提问")
    public Result<Question> update(Question question){
        iQuestionService.saveOrUpdate(question);
        return new ResultUtil<Question>().setData(question);
    }

    @RequestMapping(value = "/delByIds", method = RequestMethod.POST)
    @ApiOperation(value = "删除提问")
    public Result<Object> delByIds(@RequestParam String[] ids){
        for(String id : ids){
            iQuestionService.removeById(id);
        }
        return ResultUtil.success();
    }
    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "增改课件")
    public Result<CourseWare> saveOrUpdate(Question question){
        System.out.println("Question: "+question);
        if(iQuestionService.saveOrUpdate(question)){
            return ResultUtil.success("保存成功");
        }
        return ResultUtil.error();
    }
    /**
     * 构建查询条件
     * @param lambda
     * @param vo
     */
    private void buildCondition(LambdaQueryWrapper<Question> lambda, QuestionVo vo) {
        if(vo==null) {
            return;
        }
        if (ObjectUtils.isNotEmpty(vo.getUserId())){
            lambda.eq(Question::getUserId, vo.getUserId());
        }
        if (ObjectUtils.isNotEmpty(vo.getQuestion())){
            lambda.like(Question::getQuestion, vo.getQuestion());
        }
        if (ObjectUtils.isNotEmpty(vo.getSubjectId())){
            lambda.eq(Question::getSubjectId, vo.getSubjectId());
        }
        if (ObjectUtils.isNotEmpty(vo.getStatus())){
            lambda.eq(Question::getStatus, vo.getStatus());
        }
        //根据startTime
        if (ObjectUtils.isNotEmpty(vo.getStartTime())){
            lambda.ge(Question::getCreateTime, vo.getStartTime());
        }
        //根据endTime
        if (ObjectUtils.isNotEmpty(vo.getEndTime())){
            lambda.le(Question::getCreateTime, vo.getEndTime());
        }
        lambda.eq(Question::getDelFlag,0);
        lambda.orderByDesc(Question::getCreateTime);
    }

}
